/**
 * @file diWenFun.c
 * @author  xiaowine (xiaowine@sina.cn)
 * @brief
 * @version 01.00
 * @date    2021-05-21
 *
 * @copyright Copyright (c) {2020}  xiaowine
 *
 * @par 修改日志:
 * <table>
 * <tr><th>Date       <th>Version <th>Author  <th>Description
 * <tr><td>2021-05-21 <td>1.0     <td>wangh     <td>内容
 * </table>
 * ******************************************************************
 * *                   .::::
 * *                 .::::::::
 * *                ::::::::::
 * *             ..:::::::::::
 * *          '::::::::::::
 * *            .:::::::::
 * *       '::::::::::::::..        女神助攻,流量冲天
 * *            ..::::::::::::.     永不宕机,代码无bug
 * *          ``:::::::::::::::
 * *           ::::``:::::::::'        .:::
 * *          ::::'   ':::::'       .::::::::
 * *        .::::'      ::::     .:::::::'::::
 * *       .:::'       :::::  .:::::::::' ':::::
 * *      .::'        :::::.:::::::::'      ':::::
 * *     .::'         ::::::::::::::'         ``::::
 * * ...:::           ::::::::::::'              ``::
 * *```` ':.          ':::::::::'                  ::::.
 * *                   '.:::::'                    ':'````.
 * ******************************************************************
 */

/*================== 头文件包含			==============================*/

#include "sys.h"
#include "diWenFun.h"
#include <string.h>
#include "T5L_lib.h"

/*================== 宏定义声明			==============================*/
//#define

/*================== 数据类型声明		==============================*/
// typedef struct enum union

/*================== 外部变量声明		==============================*/

/*================== 外部函数声明		==============================*/

//复位重启系统
void resetSys(void)
{
    const uint8_t restaSys[4] = {0x55, 0xAA, 0x5A, 0xA5};
    write_dgusii_vp(0x04, restaSys, 4);  //重启系统
}

/*=============================================================================
  功   能: 配置厂商信息和WIFI信息
  参   数:	前面三个参数由 迪文云平台 提供
                        inDevProduceID  --> 设备生产商 ID
                        inDevclassify  --> 每个生产商设备的分类
                        inDevNumberAddr  --> 单个设备编号

                        后面三个参数自己根据情况修改
                        wifiName  --> WIFI名字，官方说要求不能有中文
                        wifiPassword  --> WIFI密码
                        devInfoTxt  --> 设备描述信息
  返   回: 无
  描   述: 无
=============================================================================*/
void configCloudAndWIFIinfo(uint32_t inDevProduceID, uint16_t inDevclassify, uint32_t inDevNumberAddr, char *wifiName,
                            char *wifiPassword, char *devInfoTxt)
{
    uint8_t xdata CloudAndWIFI[64];

    memset(CloudAndWIFI, 0, sizeof CloudAndWIFI);
    write_dgusii_vp(0x410, CloudAndWIFI, 12);  //擦除厂商信息
    // write_dgusii_vp(0x4B0, CloudAndWIFI, 64);  //擦除WIFI信息
    //    ReadDGUS(0x0016, (u8 *)&cache, 2);

    CloudAndWIFI[0] = 0x5A;  //表示设备描述有效
    CloudAndWIFI[1] = 0x45;  //设备描述文本的编码方式和长度

    // 设备生产商 ID
    CloudAndWIFI[2] = (inDevProduceID & 0xFF000000) >> 24;
    CloudAndWIFI[3] = (inDevProduceID & 0xFF0000) >> 16;
    CloudAndWIFI[4] = (inDevProduceID & 0xFF00) >> 8;
    CloudAndWIFI[5] = (inDevProduceID & 0xFF);

    // 每个生产商设备的分类
    CloudAndWIFI[6] = (inDevclassify & 0xFF00) >> 8;
    CloudAndWIFI[7] = inDevclassify & 0xFF;

    // 单个设备编号
    CloudAndWIFI[8]  = (inDevNumberAddr & 0xFF000000) >> 24;
    CloudAndWIFI[9]  = (inDevNumberAddr & 0xFF0000) >> 16;
    CloudAndWIFI[10] = (inDevNumberAddr & 0xFF00) >> 8;
    CloudAndWIFI[11] = (inDevNumberAddr & 0xFF);

    //设备描述文本
    strncpy(&CloudAndWIFI[12], devInfoTxt, (strlen(devInfoTxt) < 52) ? strlen(devInfoTxt) : 52);

    write_dgusii_vp(0x410, CloudAndWIFI, sizeof CloudAndWIFI);
    // print("写入厂商信息完成：");
    // printf_hex8(CloudAndWIFI, 12, 0, 1);

    memset(CloudAndWIFI, 0, sizeof CloudAndWIFI);
    strncpy(&CloudAndWIFI[0], wifiName, strlen(wifiName));
    strncpy(&CloudAndWIFI[32], wifiPassword, strlen(wifiPassword));
    // write_dgusii_vp(0x4B0, CloudAndWIFI, sizeof CloudAndWIFI);
    // print("写入WIFI信息完成\n\n");
}
